package com.turing.post.common.impl;

import java.math.BigInteger;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.SysOpAcl;
import com.turing.post.common.util.SysOpAclDao;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * @author zxp
 * @Dec 17, 2012
 */
@Stateless(mappedName = "common.impl.SysOpAclDaoImpl")
@Remote
@Local
public class SysOpAclDaoImpl implements SysOpAclDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insertSysOpAcl(SysOpAcl sysOpAcl)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, sysOpAcl);
	}

	@Override
	public int deleteSysOpAcl(BigInteger roleId)
			throws PCPSApplicationException {
		Query query = _em.createNativeQuery(
				"delete from SysOpAcl where roleid=?1").setParameter(1, roleId);
		return query.executeUpdate();
	}

	@Override
	public SysOpAcl getSysOpAclByPrimaryKey(SysOpAcl sysOpAcl) {
		return _em.find(SysOpAcl.class, sysOpAcl);
	}

	@Override
	public void deleteSysOpAclByMenuGrpId(Integer menuGrpId, BigInteger roleId) {
		String sql = "delete from sysopacl where sapid in("
				+ "select sapid from sysopsap s,(select menuid from sysopmenu where  menutype='1' and supermenuid in "
				+ "(select menuid from sysopmenu where menutype='0' and supermenuid=?1)) b where s.menuid=b.menuid) and roleid=?2";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, menuGrpId);
		query.setParameter(2, roleId);
		query.executeUpdate();
	}
}
